package com.rmt.www.serviceImpl;

import java.util.List;

import oracle.net.aso.d;

import com.opensymphony.xwork2.ActionContext;
import com.rmt.www.constant.ArtifactStatus;
import com.rmt.www.constant.RequestType;
import com.rmt.www.dao.*;
import com.rmt.www.service.AdminManageChangeService;
import com.rmt.www.vo.DesignArtifact;
import com.rmt.www.vo.DesignArtifactHistory;
import com.rmt.www.vo.DesignArtifactRequest;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.FeatureArtifactHistory;
import com.rmt.www.vo.FeatureArtifactRequest;
import com.rmt.www.vo.ImplementationArtifact;
import com.rmt.www.vo.ImplementationArtifactHistory;
import com.rmt.www.vo.ImplementationArtifactRequest;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.RequirementArtifact;
import com.rmt.www.vo.RequirementArtifactHistory;
import com.rmt.www.vo.RequirementArtifactRequest;
import com.rmt.www.vo.TestArtifact;
import com.rmt.www.vo.TestArtifactHistory;
import com.rmt.www.vo.TestArtifactRequest;
import com.rmt.www.vo.User;

public class AdminManageChangeServiceImpl implements AdminManageChangeService {

	private FeatureArtifactDao featureArtifactDao;
	private FeatureArtifactRequestDao featureArtifactRequestDao;
	private RequirementArtifactDao requirementArtifactDao;
	private RequirementArtifactRequestDao requirementArtifactRequestDao;
	private DesignArtifactRequestDao designArtifactRequestDao;
	private DesignArtifactDao designArtifactDao;
	private ImplementationArtifactDao implementationArtifactDao;
	private ImplementationArtifactRequestDao implementationArtifactRequestDao;
	private TestArtifactDao testArtifactDao;
	private TestArtifactRequestDao testArtifactRequestDao;
	
	private UserDao userDao;
	private ProjectDao projectDao;
	private DesignArtifactHistoryDao designArtifactHistoryDao;
	private FeatureArtifactHistoryDao featureArtifactHistoryDao;
	private RequirementArtifactHistoryDao requirementArtifactHistoryDao;
	private TestArtifactHistoryDao testArtifactHistoryDao;
	private ImplementationArtifactHistoryDao implementationArtifactHistoryDao;
	
	public DesignArtifactHistoryDao getDesignArtifactHistoryDao() {
		return designArtifactHistoryDao;
	}

	public void setDesignArtifactHistoryDao(
			DesignArtifactHistoryDao designArtifactHistoryDao) {
		this.designArtifactHistoryDao = designArtifactHistoryDao;
	}

	public void acceptDesignRequest(int rid) {
		DesignArtifactRequest r = designArtifactRequestDao.getDesignArtifactRequestById(rid);
		if (r.getRequesttype().equalsIgnoreCase(RequestType.ADD)) {
			DesignArtifact da = new DesignArtifact();
			da.setId(null);
			da.setTitle(r.getTitle());
			da.setPid(r.getPid());
			da.setPriority(r.getPriority());
			da.setEffort(r.getEffort());
			da.setCost(r.getCost());
			da.setRisk(r.getRisk());
			da.setComment(r.getComment());
			da.setUpdatedtimes(r.getUpdatedtimes());
			da.setVersion(r.getVersion());
			da.setStatus(r.getStatus());
			da.setConsideration(r.getConsideration());
			da.setInput(r.getInput());
			da.setFunction(r.getFunction());
			da.setOutput(r.getOutput());
			da.setPerfrequest(r.getPerfrequest());
			da.setSecurityrequest(r.getSecurityrequest());
			da.setStoragerequest(r.getStoragerequest());
			da.setInnerinterface(r.getInnerinterface());
			da.setOuterinterface(r.getOuterinterface());
			da.setArchitect(r.getArchitect());
			da.setCreaterid(r.getCreaterid());
			da.setUpdaterid(r.getUpdaterid());
			da.setCreatedtime(r.getCreatedtime());
			da.setUpdatedtime(r.getUpdatedtime());
			
			int lastinsertid = designArtifactDao.insertDesignArtifact(da);
			
			DesignArtifactHistory h = new DesignArtifactHistory();
			h.setId(null);
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());	//added
			h.setConsideration(r.getConsideration());
			h.setInput(r.getInput());
			h.setFunction(r.getFunction());
			h.setOutput(r.getOutput());
			h.setPerfrequest(r.getPerfrequest());
			h.setSecurityrequest(r.getSecurityrequest());
			h.setStoragerequest(r.getStoragerequest());
			h.setInnerinterface(r.getInnerinterface());
			h.setOuterinterface(r.getOuterinterface());
			h.setArchitect(r.getArchitect());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDesignartifactid(lastinsertid);
			
			designArtifactHistoryDao.insertDesignArtifactHistory(h);
			
			designArtifactRequestDao.deleteDesignArtifactRequestById(rid);
			
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.UPDATE)) {
			DesignArtifact da = new DesignArtifact();
			da.setPid(r.getPid());
			da.setTitle(r.getTitle());
			da.setPriority(r.getPriority());
			da.setEffort(r.getEffort());
			da.setCost(r.getCost());
			da.setRisk(r.getRisk());
			da.setComment(r.getComment());
			da.setUpdatedtimes(r.getUpdatedtimes()+1);
			da.setVersion(r.getVersion());
			da.setStatus(r.getStatus());
			da.setConsideration(r.getConsideration());
			da.setInput(r.getInput());
			da.setFunction(r.getFunction());
			da.setOutput(r.getOutput());
			da.setPerfrequest(r.getPerfrequest());
			da.setSecurityrequest(r.getSecurityrequest());
			da.setStoragerequest(r.getStoragerequest());
			da.setInnerinterface(r.getInnerinterface());
			da.setOuterinterface(r.getOuterinterface());
			da.setArchitect(r.getArchitect());
			da.setCreaterid(r.getCreaterid());
			da.setUpdaterid(r.getUpdaterid());
			da.setCreatedtime(r.getCreatedtime());
			da.setUpdatedtime(r.getUpdatedtime());
			
			designArtifactDao.updateDesignArtifact(da);
			
			DesignArtifactHistory h = new DesignArtifactHistory();
			h.setPid(r.getPid());
			da.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes()+1);
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			h.setConsideration(r.getConsideration());
			h.setInput(r.getInput());
			h.setFunction(r.getFunction());
			h.setOutput(r.getOutput());
			h.setPerfrequest(r.getPerfrequest());
			h.setSecurityrequest(r.getSecurityrequest());
			h.setStoragerequest(r.getStoragerequest());
			h.setInnerinterface(r.getInnerinterface());
			h.setOuterinterface(r.getOuterinterface());
			h.setArchitect(r.getArchitect());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDesignartifactid(r.getId());
			
			designArtifactHistoryDao.insertDesignArtifactHistory(h);
			
			designArtifactRequestDao.deleteDesignArtifactRequestById(rid);
			
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.DELETE)) {
			designArtifactDao.deleteDesignArtifactById(r.getDesignartifactid());
			DesignArtifactHistory h = new DesignArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes()+1);
			h.setVersion(r.getVersion());
			h.setStatus(ArtifactStatus.DELETED);
			h.setConsideration(r.getConsideration());
			h.setInput(r.getInput());
			h.setFunction(r.getFunction());
			h.setOutput(r.getOutput());
			h.setPerfrequest(r.getPerfrequest());
			h.setSecurityrequest(r.getSecurityrequest());
			h.setStoragerequest(r.getStoragerequest());
			h.setInnerinterface(r.getInnerinterface());
			h.setOuterinterface(r.getOuterinterface());
			h.setArchitect(r.getArchitect());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDesignartifactid(r.getId());
			
			designArtifactHistoryDao.insertDesignArtifactHistory(h);
			designArtifactRequestDao.deleteDesignArtifactRequestById(rid);
		}
	}

	public void acceptFeatureRequest(int rid) {
		FeatureArtifactRequest r = featureArtifactRequestDao.getFeatureArtifactRequestById(rid);
		if (r.getRequesttype().equalsIgnoreCase(RequestType.ADD)) {
			FeatureArtifact fa = new FeatureArtifact();
			fa.setPid(r.getPid());
			fa.setTitle(r.getTitle());
			fa.setVersion(r.getVersion());
			fa.setPriority(r.getPriority());
			fa.setSource(r.getSource());
			fa.setRational(r.getRational());
			fa.setStatus(r.getStatus());
			fa.setCreatedtime(r.getCreatedtime());
			fa.setUpdatedtime(r.getUpdatedtime());
			fa.setDescription(r.getDescription());
			fa.setCost(r.getCost());
			fa.setEffort(r.getEffort());
			fa.setRisk(r.getRisk());
			fa.setUpdatedtimes(r.getUpdatedtimes());
			fa.setAuthorId(r.getAuthorId());
			fa.setUpdateUserId(r.getUpdateUserId());
			
			int lastinsertid = featureArtifactDao.insertFeatureArtifact(fa);
			
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setFeatureartifactid(lastinsertid);
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
			
			featureArtifactRequestDao.deleteFeatureArtifactRequestById(rid);
			
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.UPDATE)) {
			FeatureArtifact fa = new FeatureArtifact();
			fa.setPid(r.getPid());
			fa.setTitle(r.getTitle());
			fa.setVersion(r.getVersion());
			fa.setPriority(r.getPriority());
			fa.setSource(r.getSource());
			fa.setRational(r.getRational());
			fa.setStatus(r.getStatus());
			fa.setCreatedtime(r.getCreatedtime());
			fa.setUpdatedtime(r.getUpdatedtime());
			fa.setDescription(r.getDescription());
			fa.setCost(r.getCost());
			fa.setEffort(r.getEffort());
			fa.setRisk(r.getRisk());
			fa.setUpdatedtimes(r.getUpdatedtimes()+1);
			fa.setAuthorId(r.getAuthorId());
			fa.setUpdateUserId(r.getUpdateUserId());
			
			featureArtifactDao.updateFeatureArtifact(fa);
			
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes()+1);
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setFeatureartifactid(r.getFeatureartifactid());
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
			
			featureArtifactRequestDao.deleteFeatureArtifactRequestById(rid);
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.DELETE)) {
			featureArtifactDao.deleteFeatureArtifactById(r.getFeatureartifactid());
			//insert into history
			FeatureArtifactHistory h = new FeatureArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(ArtifactStatus.DELETED);		//mark as deleted
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes()+1);
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setFeatureartifactid(r.getFeatureartifactid());
			featureArtifactHistoryDao.insertFeatureArtifactHistory(h);
			featureArtifactRequestDao.deleteFeatureArtifactRequestById(rid);
		} else {
			
		}
	}

	public void acceptImplRequest(int rid) {
		ImplementationArtifactRequest r = implementationArtifactRequestDao.getImplementationArtifactRequestById(rid);
		if (r.getRequesttype().equalsIgnoreCase(RequestType.ADD)) {
			ImplementationArtifact ia = new ImplementationArtifact();
			
			ia.setPid(r.getPid());
			ia.setTitle(r.getTitle());
			ia.setFunction(r.getFunction());
			ia.setInput(r.getInput());
			ia.setOutput(r.getOutput());
			ia.setProcessingsteps(r.getProcessingsteps());
			ia.setAlgorithm(r.getAlgorithm());
			ia.setErrorhandling(r.getErrorhandling());
			ia.setLanguage(r.getLanguage());
			ia.setCode(r.getCode());
			ia.setPriority(r.getPriority());
			ia.setEffort(r.getEffort());
			ia.setCost(r.getCost());
			ia.setRisk(r.getRisk());
			ia.setComment(r.getComment());
			ia.setUpdatedtimes(r.getUpdatedtimes());
			ia.setVersion(r.getVersion());
			ia.setStatus(r.getStatus());
			ia.setCreaterid(r.getCreaterid());
			ia.setCreatedtime(r.getCreatedtime());
			ia.setUpdaterid(r.getUpdaterid());
			ia.setUpdatedtime(r.getUpdatedtime());
			
			int lastinsertid = implementationArtifactDao.insertImplementationArtifact(ia);
			
			ImplementationArtifactHistory h = new ImplementationArtifactHistory();
			h.setPid(r.getPid());
			h.setFunction(r.getFunction());
			h.setTitle(r.getTitle());
			h.setInput(r.getInput());
			h.setOutput(r.getOutput());
			h.setProcessingsteps(r.getProcessingsteps());
			h.setAlgorithm(r.getAlgorithm());
			h.setErrorhandling(r.getErrorhandling());
			h.setLanguage(r.getLanguage());
			h.setCode(r.getCode());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdaterid(r.getUpdaterid());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setImplartifactid(lastinsertid);
			
			implementationArtifactHistoryDao.insertImplementationArtifactHistory(h);
			
			implementationArtifactRequestDao.deleteImplementationArtifactRequestById(rid);
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.UPDATE)) {
			ImplementationArtifact ia = new ImplementationArtifact();
			
			ia.setPid(r.getPid());
			ia.setFunction(r.getFunction());
			ia.setTitle(r.getTitle());
			ia.setInput(r.getInput());
			ia.setOutput(r.getOutput());
			ia.setProcessingsteps(r.getProcessingsteps());
			ia.setAlgorithm(r.getAlgorithm());
			ia.setErrorhandling(r.getErrorhandling());
			ia.setLanguage(r.getLanguage());
			ia.setCode(r.getCode());
			ia.setPriority(r.getPriority());
			ia.setEffort(r.getEffort());
			ia.setCost(r.getCost());
			ia.setRisk(r.getRisk());
			ia.setComment(r.getComment());
			ia.setUpdatedtimes(r.getUpdatedtimes());
			ia.setVersion(r.getVersion());
			ia.setStatus(r.getStatus());
			ia.setCreaterid(r.getCreaterid());
			ia.setCreatedtime(r.getCreatedtime());
			ia.setUpdaterid(r.getUpdaterid());
			ia.setUpdatedtime(r.getUpdatedtime());
			
			implementationArtifactDao.updateImplementationArtifact(ia);
			
			ImplementationArtifactHistory h = new ImplementationArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setFunction(r.getFunction());
			h.setInput(r.getInput());
			h.setOutput(r.getOutput());
			h.setProcessingsteps(r.getProcessingsteps());
			h.setAlgorithm(r.getAlgorithm());
			h.setErrorhandling(r.getErrorhandling());
			h.setLanguage(r.getLanguage());
			h.setCode(r.getCode());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdaterid(r.getUpdaterid());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setImplartifactid(r.getImplartifactid());
			
			implementationArtifactHistoryDao.insertImplementationArtifactHistory(h);
			
			implementationArtifactRequestDao.deleteImplementationArtifactRequestById(rid);
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.DELETE)) {
			ImplementationArtifactHistory h = new ImplementationArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setFunction(r.getFunction());
			h.setInput(r.getInput());
			h.setOutput(r.getOutput());
			h.setProcessingsteps(r.getProcessingsteps());
			h.setAlgorithm(r.getAlgorithm());
			h.setErrorhandling(r.getErrorhandling());
			h.setLanguage(r.getLanguage());
			h.setCode(r.getCode());
			h.setPriority(r.getPriority());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdaterid(r.getUpdaterid());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setImplartifactid(r.getImplartifactid());
			
			implementationArtifactHistoryDao.insertImplementationArtifactHistory(h);
			
			implementationArtifactRequestDao.deleteImplementationArtifactRequestById(rid);
		} else {
			
		}
	}

	public void acceptRequirementRequest(int rid) {
		RequirementArtifactRequest r = requirementArtifactRequestDao.getRequirementArtifactRequestById(rid);
		if (r.getRequesttype().equalsIgnoreCase(RequestType.ADD)) {
			RequirementArtifact ra = new RequirementArtifact();
			ra.setPid(r.getPid());
			ra.setTitle(r.getTitle());
			ra.setVersion(r.getVersion());
			ra.setPriority(r.getPriority());
			ra.setSource(r.getSource());
			ra.setRational(r.getRational());
			ra.setStatus(r.getStatus());
			ra.setDescription(r.getDescription());
			ra.setCost(r.getCost());
			ra.setEffort(r.getEffort());
			ra.setRisk(r.getRisk());
			ra.setUpdatedtimes(r.getUpdatedtimes());
			ra.setRequirementtype(r.getRequirementtype());
			ra.setAuthorId(r.getAuthorId());
			ra.setUpdateUserId(r.getUpdateUserId());
			ra.setCreatedtime(r.getCreatedtime());
			ra.setUpdatedtime(r.getUpdatedtime());
			
			int lastinsertid = requirementArtifactDao.insertRequirementArtifact(ra);
			
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setRequirementtype(r.getRequirementtype());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setRequirementartifactid(lastinsertid);
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
			
			requirementArtifactRequestDao.deleteRequirementArtifactRequestById(rid);
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.UPDATE)) {
			RequirementArtifact ra = new RequirementArtifact();
			ra.setPid(r.getPid());
			ra.setTitle(r.getTitle());
			ra.setVersion(r.getVersion());
			ra.setPriority(r.getPriority());
			ra.setSource(r.getSource());
			ra.setRational(r.getRational());
			ra.setStatus(r.getStatus());
			ra.setDescription(r.getDescription());
			ra.setCost(r.getCost());
			ra.setEffort(r.getEffort());
			ra.setRisk(r.getRisk());
			ra.setUpdatedtimes(r.getUpdatedtimes());
			ra.setRequirementtype(r.getRequirementtype());
			ra.setAuthorId(r.getAuthorId());
			ra.setUpdateUserId(r.getUpdateUserId());
			ra.setCreatedtime(r.getCreatedtime());
			ra.setUpdatedtime(r.getUpdatedtime());
			
			requirementArtifactDao.updateRequirementArtifact(ra);
			
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(r.getStatus());
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setRequirementtype(r.getRequirementtype());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setRequirementartifactid(r.getRequirementartifactid());
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
			
			requirementArtifactRequestDao.deleteRequirementArtifactRequestById(rid);
		} else if (r.getRequesttype().equalsIgnoreCase(RequestType.DELETE)) {
			requirementArtifactDao.deleteRequirementArtifactById(r.getRequirementartifactid());
			
			RequirementArtifactHistory h = new RequirementArtifactHistory();
			h.setPid(r.getPid());
			h.setTitle(r.getTitle());
			h.setVersion(r.getVersion());
			h.setPriority(r.getPriority());
			h.setSource(r.getSource());
			h.setRational(r.getRational());
			h.setStatus(ArtifactStatus.DELETED);		//mark as deleted
			h.setDescription(r.getDescription());
			h.setCost(r.getCost());
			h.setEffort(r.getEffort());
			h.setRisk(r.getRisk());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setRequirementtype(r.getRequirementtype());
			h.setAuthorId(r.getAuthorId());
			h.setUpdateUserId(r.getUpdateUserId());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setRequirementartifactid(r.getRequirementartifactid());
			
			requirementArtifactHistoryDao.insertRequirementArtifactHistory(h);
			
			requirementArtifactRequestDao.deleteRequirementArtifactRequestById(rid);
		} else {
			
		}
	}

	public void acceptTestCaseRequest(int rid) {
		TestArtifactRequest r = testArtifactRequestDao.getTestArtifactRequestByRId(rid);
		if (r.getRequestType().equalsIgnoreCase(RequestType.ADD)) {
			TestArtifact ta = new TestArtifact();
			ta.setPid(r.getPid());
			ta.setTestcasetype(r.getTestcasetype());
			ta.setTitle(r.getTitle());
			ta.setPriority(r.getPriority());
			ta.setPrecondition(r.getPrecondition());
			ta.setInput(r.getInput());
			ta.setTestdata(r.getTestdata());
			ta.setSteps(r.getSteps());
			ta.setExpectedresult(r.getExpectedresult());
			ta.setPostcondition(r.getPostcondition());
			ta.setEffort(r.getEffort());
			ta.setCost(r.getCost());
			ta.setRisk(r.getRisk());
			ta.setComment(r.getComment());
			ta.setUpdatedtimes(r.getUpdatedtimes());
			ta.setVersion(r.getVersion());
			ta.setStatus(r.getStatus());
			//r.setTestartifactid(testartifactid);
			ta.setUpdaterid(r.getUpdaterid());
			ta.setCreaterid(r.getCreaterid());
			ta.setCreatedtime(r.getCreatedtime());
			ta.setUpdatedtime(r.getUpdatedtime());
			
			int lastinsertid = testArtifactDao.insertTestArtifact(ta);
			
			TestArtifactHistory h = new TestArtifactHistory();
			h.setPid(r.getPid());
			h.setTestcasetype(r.getTestcasetype());
			h.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setPrecondition(r.getPrecondition());
			h.setInput(r.getInput());
			h.setTestdata(r.getTestdata());
			h.setSteps(r.getSteps());
			h.setExpectedresult(r.getExpectedresult());
			h.setPostcondition(r.getPostcondition());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			//r.setTestartifactid(testartifactid);
			h.setUpdaterid(r.getUpdaterid());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setTestartifactid(lastinsertid);
			
			testArtifactHistoryDao.insertTestArtifactHistory(h);
			
			testArtifactRequestDao.deleteTestArtifactRequestByRId(rid);
		} else if (r.getRequestType().equalsIgnoreCase(RequestType.UPDATE)) {
			TestArtifact ta = new TestArtifact();
			ta.setPid(r.getPid());
			ta.setTestcasetype(r.getTestcasetype());
			ta.setTitle(r.getTitle());
			ta.setPriority(r.getPriority());
			ta.setPrecondition(r.getPrecondition());
			ta.setInput(r.getInput());
			ta.setTestdata(r.getTestdata());
			ta.setSteps(r.getSteps());
			ta.setExpectedresult(r.getExpectedresult());
			ta.setPostcondition(r.getPostcondition());
			ta.setEffort(r.getEffort());
			ta.setCost(r.getCost());
			ta.setRisk(r.getRisk());
			ta.setComment(r.getComment());
			ta.setUpdatedtimes(r.getUpdatedtimes());
			ta.setVersion(r.getVersion());
			ta.setStatus(r.getStatus());
			//r.setTestartifactid(testartifactid);
			ta.setUpdaterid(r.getUpdaterid());
			ta.setCreaterid(r.getCreaterid());
			ta.setCreatedtime(r.getCreatedtime());
			ta.setUpdatedtime(r.getUpdatedtime());
			
			testArtifactDao.updateTestArtifact(ta);
			
			TestArtifactHistory h = new TestArtifactHistory();
			h.setPid(r.getPid());
			h.setTestcasetype(r.getTestcasetype());
			h.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setPrecondition(r.getPrecondition());
			h.setInput(r.getInput());
			h.setTestdata(r.getTestdata());
			h.setSteps(r.getSteps());
			h.setExpectedresult(r.getExpectedresult());
			h.setPostcondition(r.getPostcondition());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(r.getStatus());
			//r.setTestartifactid(testartifactid);
			h.setUpdaterid(r.getUpdaterid());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setTestartifactid(r.getTestartifactid());
			
			testArtifactHistoryDao.insertTestArtifactHistory(h);
			
			testArtifactRequestDao.deleteTestArtifactRequestByRId(rid);
		} else if (r.getRequestType().equalsIgnoreCase(RequestType.DELETE)) {
			TestArtifactHistory h = new TestArtifactHistory();
			h.setPid(r.getPid());
			h.setTestcasetype(r.getTestcasetype());
			h.setTitle(r.getTitle());
			h.setPriority(r.getPriority());
			h.setPrecondition(r.getPrecondition());
			h.setInput(r.getInput());
			h.setTestdata(r.getTestdata());
			h.setSteps(r.getSteps());
			h.setExpectedresult(r.getExpectedresult());
			h.setPostcondition(r.getPostcondition());
			h.setEffort(r.getEffort());
			h.setCost(r.getCost());
			h.setRisk(r.getRisk());
			h.setComment(r.getComment());
			h.setUpdatedtimes(r.getUpdatedtimes());
			h.setVersion(r.getVersion());
			h.setStatus(ArtifactStatus.DELETED);
			//r.setTestartifactid(testartifactid);
			h.setUpdaterid(r.getUpdaterid());
			h.setCreaterid(r.getCreaterid());
			h.setCreatedtime(r.getCreatedtime());
			h.setUpdatedtime(r.getUpdatedtime());
			h.setTestartifactid(r.getTestartifactid());
			
			testArtifactHistoryDao.insertTestArtifactHistory(h);
			testArtifactRequestDao.deleteTestArtifactRequestByRId(rid);
		} else {
			
		}
	}

	public void declineDesignRequest(int rid) {
		designArtifactRequestDao.deleteDesignArtifactRequestById(rid);
	}

	public void declineFeatureRequest(int rid) {
		featureArtifactRequestDao.deleteFeatureArtifactRequestById(rid);
	}

	public void declineImplRequest(int rid) {
		implementationArtifactRequestDao.deleteImplementationArtifactRequestById(rid);
	}

	public void declineRequirementRequest(int rid) {
		requirementArtifactRequestDao.deleteRequirementArtifactRequestById(rid);
	}

	public void declineTestCaseRequest(int rid) {
		testArtifactRequestDao.deleteTestArtifactRequestByRId(rid);
	}

	public List<DesignArtifactRequest> getProjectDesignRequests(int pid) {
		List<DesignArtifactRequest> list = designArtifactRequestDao.getDesignArtifactRequestByPId(pid);
		for (int i = 0; i < list.size(); i++) {
			DesignArtifactRequest request = list.get(i);
			request.set_authorname(userDao.getUser(request.getCreaterid()).getName());
			if (request.getUpdaterid() != 0) {
				request.set_updatername(userDao.getUser(request.getUpdaterid()).getName());
			}
		}
		return list;
	}

	public List<FeatureArtifactRequest> getProjectFeatureRequests(int pid) {
		List<FeatureArtifactRequest> list = featureArtifactRequestDao.getFeatureArtifactRequestsByPId(pid);
		for (int i = 0; i < list.size(); i++) {
			FeatureArtifactRequest request = list.get(i);
			request.set_authorname(userDao.getUser(request.getAuthorId()).getName());
			if (request.getUpdateUserId() != 0) {
				request.set_updatername(userDao.getUser(request.getUpdateUserId()).getName());
			}
		}
		return list;
	}

	public List<ImplementationArtifactRequest> getProjectImplementation(int pid) {
		List<ImplementationArtifactRequest> list = implementationArtifactRequestDao.getImplementationArtifactRequestsByPId(pid);
		for (int i = 0; i < list.size(); i++) {
			ImplementationArtifactRequest request = list.get(i);
			request.set_authorname(userDao.getUser(request.getCreaterid()).getName());
			if (request.getUpdaterid() != 0) {
				request.set_updatername(userDao.getUser(request.getUpdaterid()).getName());
			}
		}
		return list;

	}

	public List<RequirementArtifactRequest> getProjectRequirementRequest(int pid) {
		List<RequirementArtifactRequest> list = requirementArtifactRequestDao.getRequirementArtifactRequestsByPId(pid);
		for (int i = 0; i < list.size(); i++) {
			RequirementArtifactRequest request = list.get(i);
			request.set_authorname(userDao.getUser(request.getAuthorId()).getName());
			if (request.getUpdateUserId() != 0) {
				request.set_updatername(userDao.getUser(request.getUpdateUserId()).getName());
			}
		}
		return list;

	}

	public List<TestArtifactRequest> getProjectTestCase(int pid) {
		List<TestArtifactRequest> list = testArtifactRequestDao.getTestArtifactRequestsByPId(pid);
		for (int i = 0; i < list.size(); i++) {
			TestArtifactRequest request = list.get(i);
			request.set_authorname(userDao.getUser(request.getCreaterid()).getName());
			if (request.getUpdaterid() != 0) {
				request.set_updatername(userDao.getUser(request.getUpdaterid()).getName());
			}
		}
		return list;

	}

	public FeatureArtifactDao getFeatureArtifactDao() {
		return featureArtifactDao;
	}

	public void setFeatureArtifactDao(FeatureArtifactDao featureArtifactDao) {
		this.featureArtifactDao = featureArtifactDao;
	}

	public FeatureArtifactRequestDao getFeatureArtifactRequestDao() {
		return featureArtifactRequestDao;
	}

	public void setFeatureArtifactRequestDao(
			FeatureArtifactRequestDao featureArtifactRequestDao) {
		this.featureArtifactRequestDao = featureArtifactRequestDao;
	}

	public RequirementArtifactDao getRequirementArtifactDao() {
		return requirementArtifactDao;
	}

	public void setRequirementArtifactDao(
			RequirementArtifactDao requirementArtifactDao) {
		this.requirementArtifactDao = requirementArtifactDao;
	}

	public RequirementArtifactRequestDao getRequirementArtifactRequestDao() {
		return requirementArtifactRequestDao;
	}

	public void setRequirementArtifactRequestDao(
			RequirementArtifactRequestDao requirementArtifactRequestDao) {
		this.requirementArtifactRequestDao = requirementArtifactRequestDao;
	}

	public DesignArtifactRequestDao getDesignArtifactRequestDao() {
		return designArtifactRequestDao;
	}

	public void setDesignArtifactRequestDao(
			DesignArtifactRequestDao designArtifactRequestDao) {
		this.designArtifactRequestDao = designArtifactRequestDao;
	}

	public DesignArtifactDao getDesignArtifactDao() {
		return designArtifactDao;
	}

	public void setDesignArtifactDao(DesignArtifactDao designArtifactDao) {
		this.designArtifactDao = designArtifactDao;
	}

	public ImplementationArtifactDao getImplementationArtifactDao() {
		return implementationArtifactDao;
	}

	public void setImplementationArtifactDao(
			ImplementationArtifactDao implementationArtifactDao) {
		this.implementationArtifactDao = implementationArtifactDao;
	}

	public ImplementationArtifactRequestDao getImplementationArtifactRequestDao() {
		return implementationArtifactRequestDao;
	}

	public void setImplementationArtifactRequestDao(
			ImplementationArtifactRequestDao implementationArtifactRequestDao) {
		this.implementationArtifactRequestDao = implementationArtifactRequestDao;
	}

	public TestArtifactDao getTestArtifactDao() {
		return testArtifactDao;
	}

	public void setTestArtifactDao(TestArtifactDao testArtifactDao) {
		this.testArtifactDao = testArtifactDao;
	}

	public TestArtifactRequestDao getTestArtifactRequestDao() {
		return testArtifactRequestDao;
	}

	public void setTestArtifactRequestDao(
			TestArtifactRequestDao testArtifactRequestDao) {
		this.testArtifactRequestDao = testArtifactRequestDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public ProjectDao getProjectDao() {
		return projectDao;
	}

	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	public String getProjectName(int pid) {
		return projectDao.getProject(pid).getTitle();
	}

	public FeatureArtifactHistoryDao getFeatureArtifactHistoryDao() {
		return featureArtifactHistoryDao;
	}

	public void setFeatureArtifactHistoryDao(
			FeatureArtifactHistoryDao featureArtifactHistoryDao) {
		this.featureArtifactHistoryDao = featureArtifactHistoryDao;
	}

	public RequirementArtifactHistoryDao getRequirementArtifactHistoryDao() {
		return requirementArtifactHistoryDao;
	}

	public void setRequirementArtifactHistoryDao(
			RequirementArtifactHistoryDao requirementArtifactHistoryDao) {
		this.requirementArtifactHistoryDao = requirementArtifactHistoryDao;
	}

	public TestArtifactHistoryDao getTestArtifactHistoryDao() {
		return testArtifactHistoryDao;
	}

	public void setTestArtifactHistoryDao(
			TestArtifactHistoryDao testArtifactHistoryDao) {
		this.testArtifactHistoryDao = testArtifactHistoryDao;
	}

	public ImplementationArtifactHistoryDao getImplementationArtifactHistoryDao() {
		return implementationArtifactHistoryDao;
	}

	public void setImplementationArtifactHistoryDao(
			ImplementationArtifactHistoryDao implementationArtifactHistoryDao) {
		this.implementationArtifactHistoryDao = implementationArtifactHistoryDao;
	}

	public void createBaseline(int pid) {
		Project project = projectDao.getProject(pid);
		project.setIsbaselinecreated(true);
		projectDao.updateProject(project);
	}

}
